package com.wson.function.compass;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.wson.function.basic.BasicFunc;
import com.wson.function.wolfow.ActiveEventsFunc;
import com.wson.model.basic.IBasicView;
import com.wson.model.compass.status.ICompassStatusView;
import com.wson.model.compass.status.impl.CompassStatusModel;
import com.wson.model.wolfow.event.IWolfEventView;

public class ActiveStatusFunc extends BasicFunc{
	public List<ICompassStatusView> function(Long userID)throws Exception{
		return function(userID, true);
	}
	public List<ICompassStatusView> function(Long userID, boolean withstatic)throws Exception{
		List<ICompassStatusView> results = new ArrayList<ICompassStatusView>();
		
		ActiveEventsFunc efunc = new ActiveEventsFunc();
		this.initFunc(efunc);
		List<IWolfEventView> events = efunc.function(userID);
		//System.out.println("events "+events);
		if(events!=null){
			System.out.println("events "+events.size());
			
			for(IWolfEventView event : events){				
				
				if(event==null 
						|| event.getInternalKey()==null)
					continue;
				CompassStatusModel status = new CompassStatusModel();
				this.initModel((IBasicView)status);
				status.setID(Long.parseLong(event.getInternalKey()));
				
				if(status.getID()!=null 
						&& status.selfSelect()){
					//System.out.println("event.getInboundStepID() "+event.getInboundStepID());
					status.setInboundStepID(event.getInboundStepID());
					status.setOutboundEventID(event.getID());
					results.add(status);
				}
			}
		}
		
		if(withstatic){
			CompassStatusModel query = new CompassStatusModel();
			this.initModel((IBasicView)query);
			query.setCreateUserID(userID);
			Collection<IBasicView> statics =query.selfQuery();
			if(statics!=null){
				System.out.println("statics "+statics.size());
				if(statics!=null 
						&& statics.size()>0){
					for(IBasicView obj : statics)
						results.add((ICompassStatusView)obj);
				}	
			}
		}
		
		
		Collections.sort(results, new Comparator<ICompassStatusView>(){
			public int compare(ICompassStatusView arg0, ICompassStatusView arg1) {   
	               return arg1.getCreateDate().compareTo(arg0.getCreateDate());   
	            }  
		});
		System.out.println("results "+results.size());
		
		return results;
	}

}
